package org.codeart.driver.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.codeart.common.constant.DriverCarEnum;
import org.codeart.common.dto.Car;
import org.codeart.common.dto.DriverCarBindingRelationship;
import org.codeart.common.dto.DriverUser;
import org.codeart.common.dto.DriverUserWorkStatus;
import org.codeart.common.response.OrderDriverResponse;
import org.codeart.common.util.R;
import org.codeart.driver.mapper.CarMapper;
import org.codeart.driver.mapper.DriverCarBindingRelationshipMapper;
import org.codeart.driver.mapper.DriverUserMapper;
import org.codeart.driver.mapper.DriverUserWorkStatusMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author element
 * @description 针对表【tb_driver_user】的数据库操作Service
 * @createDate 2023-03-07 16:06:52
 */
@Service
public class DriverUserService {

    @Autowired
    private DriverUserMapper driverUserMapper;

    @Autowired
    private DriverUserWorkStatusMapper driverUserWorkStatusMapper;

    @Autowired
    private DriverCarBindingRelationshipMapper driverCarBindingRelationshipMapper;

    @Autowired
    private CarMapper carMapper;

    public boolean addUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtCreate(now).setGmtModified(now);
        int i = driverUserMapper.insert(driverUser);

        // 初始化司机工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverUser.getId())
                            .setWorkStatus(DriverCarEnum.WORK_STATUS_STOP.value())
                            .setGmtModified(now)
                            .setGmtCreate(now);
        int j = driverUserWorkStatusMapper.insert(driverUserWorkStatus);
        return i != -1 && j != -1;
    }

    public boolean updateUser(DriverUser driverUser) {
        LocalDateTime now = LocalDateTime.now();
        driverUser.setGmtModified(now);
        int i = driverUserMapper.updateById(driverUser);
        return i != -1;
    }

    public DriverUser getUser(String driverPhone) {
        if (StrUtil.isEmpty(driverPhone)) {
            return null;
        }
        QueryWrapper<DriverUser> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(DriverUser::getDriverPhone, driverPhone)
               .eq(DriverUser::getState, DriverCarEnum.STATE_VALID.value());
        return driverUserMapper.selectOne(wrapper);
    }


    public R<OrderDriverResponse> getAvailableDriver(Long carId) {
        QueryWrapper<DriverCarBindingRelationship> wrapper = new QueryWrapper<>();
        wrapper.lambda()
               .eq(DriverCarBindingRelationship::getCarId, carId)
               .eq(DriverCarBindingRelationship::getBindState, DriverCarEnum.BIND.value());

        DriverCarBindingRelationship one = driverCarBindingRelationshipMapper.selectOne(wrapper);
        if (one == null) {
            return R.fail("没有可用的司机");
        }
        Long driverId = one.getDriverId();

        QueryWrapper<DriverUserWorkStatus> wrapper1 = new QueryWrapper<>();
        wrapper1.lambda()
                .eq(DriverUserWorkStatus::getDriverId, driverId)
                .eq(DriverUserWorkStatus::getWorkStatus, DriverCarEnum.WORK_STATUS_START.value());

        DriverUserWorkStatus two = driverUserWorkStatusMapper.selectOne(wrapper1);
        if (two == null) {
            return R.fail("没有可用的司机");
        } else {
            // 查询司机信息
            QueryWrapper<DriverUser> wrapper2 = new QueryWrapper<>();
            wrapper2.lambda().eq(DriverUser::getId, driverId);
            DriverUser driverUser = driverUserMapper.selectOne(wrapper2);

            // 查询车辆信息
            QueryWrapper<Car> wrapper3 = new QueryWrapper<>();
            wrapper3.lambda().eq(Car::getId, carId);
            Car car = carMapper.selectOne(wrapper3);

            OrderDriverResponse response = new OrderDriverResponse();
            response.setCarId(carId)
                    .setDriverId(driverId)
                    .setDriverPhone(driverUser.getDriverPhone())
                    .setLicenseId(driverUser.getLicenseId())
                    .setVehicleNo(car.getVehicleNo())
                    .setVehicleType(car.getVehicleType());
            return R.ok(response);
        }
    }
}
